﻿@using Easy.CMS.Common.Models
@using Easy.Web.CMS
@{
    NavigationEntity homePage = Model.Navigations.Where(m => m.ParentId == "#").FirstOrDefault();
    string currentPath = Request.Path;
    if (homePage == null)
    {
        return;
    }
    int index = 0;
}
@model Easy.CMS.Common.ViewModels.NavigationWidgetViewModel
@helper DropDown(IEnumerable<NavigationEntity> items, string currentPath)
{
    if (items != null && items.Any())
    {
        <ul class="dropdown-menu">
            @foreach (NavigationEntity child in items)
            {
                IEnumerable<NavigationEntity> children = Model.Navigations.Where(m => m.ParentId == child.ID);
                if (children.Any())
                {

                    <li class="dropdown hoveropen">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">@child.Title <b class="caret right"></b></a>
                        @DropDown(children, currentPath)
                    </li>
                }
                else
                {
                    var path = Url.PathContent(child.Url);
                    <li class="@(currentPath.StartsWith(path) ? "active" : "")">
                        @Html.SmartLink(path, child.Title)
                    </li>
                }
            }
        </ul>
    }
}
<div class="widget navigation">
    <div class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @if (Model.Widget.Logo.IsNotNullAndWhiteSpace() || Model.Widget.Title.IsNotNullAndWhiteSpace())
                {
                    <a class="navbar-brand" href="~/">
                        @if (Model.Widget.Logo.IsNotNullAndWhiteSpace())
                        {
                            <img src="@Url.Content(Model.Widget.Logo)" alt="@Model.Widget.Title" class="logo" />
                        }
                        else
                        {
                            @Model.Widget.Title
                        }
                    </a>
                }
            </div>
            <div class="navbar-collapse collapse navbar-responsive-collapse">
                <ul class="nav navbar-nav">
                    @foreach (NavigationEntity item in Model.Navigations.Where(m => m.ParentId == "#"))
                    {
                        if (Model.Navigations.Any(m => m.ParentId == item.ID))
                        {
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">@item.Title <b class="caret"></b></a>
                                @DropDown(Model.Navigations.Where(m => m.ParentId == item.ID), currentPath)
                            </li>

                        }
                        else
                        {
                            var path = Url.PathContent(item.Url);
                            <li class="@(currentPath.StartsWith(path) || (currentPath == "/" && index == 0) ? "active" : "")">
                                @Html.SmartLink(path, item.Title)
                            </li>
                        }
                        index++;
                    }
                </ul>
            </div>
        </div>

    </div>
</div>
@using (Script.AtFoot())
{
    <script type="text/javascript">
        $(".hoveropen").mouseenter(function () {
            $(this).addClass("open");
        }).mouseleave(function () {
            $(this).removeClass("open");
        });
    </script>
}